
<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
 * @license    GNU/GPL
 */

// no direct access

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

/**
 * Hello World Component Controller
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class calendarsController extends JController
{

	function __construct()
	{
		parent::__construct();
		
		// Register Extra tasks
		$this->registerTask( 'add'  , 	'edit' );
		//var_dump('controller,construct');
		$data = JRequest::get( 'post' );
		//var_dump($data);
	}

	function edit()
	{
	
		JRequest::setVar( 'view', 'calendars' );
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);
		//var_dump('controller, edit');
		parent::display();

		// Checkin the weblink
		//$model = $this->getModel('calendar');
		//$model->checkout();
	}

	function save()
	{
	global $mainframe;
	//var_dump('to model for store');
	$model = $this->getModel('calendars');
	//var_dump($this);
	
	// use bookstore api to get isbn bk cover path
	// if requested in params
	$params =& JRequest::getVar('params');
	$config = &JFactory::getConfig();
	$api_on = $config->getValue('config.bookstore_api', NULL);
	/**
	*	TODO: add js validation for update and isbn 
	*/
	
	if( !empty($api_on) && $params['update_path'] > 0 && !empty($params['assoc_isbn'])){
	
		$api =& new bookstore_api();
		$external = $api->external_detail($params['assoc_isbn']);
		$params['img_path'] = $external->graphics;
		$params['update_path'] = 0;
		JRequest::setVar('params',$params);
	
	}	
	
	//var_dump('to model for store check');
    if ($model->store()) {
		//var_dump('to model for store');
        $msg = JText::_( 'Event Saved!' );
		// uncomment to create serialized db in xml (for export)
		//$this->update_xml();
    } else {
		//var_dump('not to model for store');
        $msg = JText::_( 'Error Saving Event' );
    }

    // Check the table in so it can be edited.... we are done with it anyway
    $link = 'index.php?option=com_calendar';
    $this->setRedirect($link, $msg);
	}

	/**
 * remove record(s)
 * @return void
 */
function remove()
{
    $model = $this->getModel('calendars');
    if(!$model->delete()) {
        $msg = JText::_( 'Error: One or More Events Could not be Deleted' );
    } else {
        $msg = JText::_( 'Event(s) Deleted' );
    }

    $this->setRedirect( 'index.php?option=com_calendar', $msg );
}


	function publish()
	{
		global $mainframe;

		$cid 	= JRequest::getVar( 'cid', array(0), 'post', 'array' );

		if (!is_array( $cid ) || count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}

		$model = $this->getModel('calendars');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( 'index.php?option=com_calendar' );
	}


	function unpublish()
	{
		global $mainframe;

		$cid 	= JRequest::getVar( 'cid', array(0), 'post', 'array' );

		if (!is_array( $cid ) || count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to unpublish' ) );
		}

		$model = $this->getModel('calendars');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
	
		$this->setRedirect( 'index.php?option=com_calendar' );
	
	}

	/**
 * cancel editing a record
 * @return void
 */
function cancel()
{
    $msg = JText::_( 'Operation Cancelled' );
    $this->setRedirect( 'index.php?option=com_calendar', $msg );
}


	function orderup()
	{
		$model = $this->getModel('weblink');
		$model->move(-1);

		$this->setRedirect( 'index.php?option=com_calendar');
	}

	function orderdown()
	{
		$model = $this->getModel('weblink');
		$model->move(1);

		$this->setRedirect( 'index.php?option=com_calendar');
	}

	function saveorder()
	{
		$cid 	= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$order 	= JRequest::getVar( 'order', array(0), 'post', 'array' );

		$model = $this->getModel('weblink');
		$model->saveorder($cid, $order);

		$msg = 'New ordering saved';
		$this->setRedirect( 'index.php?option=com_calendar', $msg );
	}

	function update_xml()
	{
		var_dump('update_xml function');
		$model = $this->getModel('calendars');
		$model->update_xml();
	}

    /**
     * Method to display the view
     *
     * @access    public
     */
    function display()
    {
		//$data = JRequest::get( 'post' );
		//var_dump($data);
		//var_dump('controller');
        parent::display();
    }

}

?>
